import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import {CommonService} from '../../../../services/common.service';
import {Router} from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
interface DataItem {
  update_time: any;
  task_type: any;
}

@Component({
  selector: 'app-contractextractionmodel',
  templateUrl: './contractextractionmodel.component.html',
  styleUrls: ['./contractextractionmodel.component.css']
})

export class ContractextractionmodelComponent implements OnInit {
  // 新建模型名称
  addModelName = '';
  // 新建模型描述
  addModelDescription='';
  // 新建模态框显示隐藏
  isVisible = false;
  // 模型列表
  listOfData: any = [
    // {
    //   model_id: "0003",
    //   model_name: "开放日识别模型",
    //   task_type: "实体识别",
    //   description: "ner模型，用于识别申购日",
    //   update_time: "2021/4/20 15:16:52 GMT+08:00"

    // }
  ];
  // 删除的模态框
  deleteShow = false;
  // 选择器选择的
  selectData:string = '文本分类';
  // 删除按钮拿到的ID
  deleteModelId:'';
  // 编辑描述id 控制编辑框显隐
  editId:'';
  // 编辑描述的数据
  Editdescribe:string = '';
  // 编辑名称数据
  EditModelName:string= "";
  // 编辑名称id 控制编辑框显隐
  editNameId:'';
  // 编辑请求体的id
  editModelId:'';
  // 剩余可创建标签长度
  ModelLength:number=0;
  currentItem: any = {
  }
  // 搜索框的value
  seekModelValue:any=null
  // 搜索的列表
  seekModelList:any = null
  timeCompare = (a: DataItem, b: DataItem) => {
    console.log(a)
    return Date.parse(a.update_time) - Date.parse(b.update_time)
  };
  fileFilterFn = (status: any, item: DataItem) => item.task_type == status
  constructor(private modal: NzModalService,
              public http:CommonService,
              private router:Router,
              private message: NzMessageService) { }

  ngOnInit() {
    //查询模型列表
    this.getModelList()
  }
  // 查询模型列表
  getModelList(){
    // 项目id
    let id = sessionStorage.getItem('id');
    this.http.post('/contract-extraction/model-mgr/query-all-model',{
      project_id:id
    }).then((res:any)=>{
      if(res && res.result != ''){
        console.log(res)
        this.listOfData = res.result
        this.seekModelList = res.result
        // 计算剩余可创建项目的长度
        let length = 100;
        this.ModelLength = length;
        if(this.ModelLength === 100){
          // 计算剩余可以创建的模型
          this.ModelLength =  this.ModelLength  - this.listOfData.length;
        }
      }else{
        this.message.error("模型列表为空!")
      }
    }).catch((err: any) => {
      this.message.error(err.error_msg)
    })
  }
  // 搜索列表
  seekModel(){
    this.listOfData = this.seekModelList
    let arr = []
    this.seekModelValue= this.seekModelValue.replace(/^\s+|\s+$/g,"")
    this.listOfData.forEach(e => {
      if(e.model_name.includes(this.seekModelValue)){
        arr.push(e)
      }
    });
    if(this.seekModelValue == ''){
      this.listOfData = this.seekModelList
    }else{
      this.listOfData = arr
    }
  }
  // 新建模型
  addModelList(){
    this.addModelName = this.addModelName.replace(/^\s+|\s+$/g,"")
    let newArray = this.listOfData.map(e => e.model_name);
    if (newArray.includes(this.addModelName)) {
      this.message.error(this.addModelName + '已经存在!');
      return;
    }
    let newArrayOne = this.listOfData.map(e => e.description);
    if (newArrayOne.includes(this.addModelDescription)) {
      this.message.error(this.addModelDescription + '已经存在!');
      return;
    }
    if(this.addModelName === ''){
      return this.message.error('新建模型输入框不能为空!')
    }else{
       // 项目id
       let id = sessionStorage.getItem('id');
       this.http.post('/contract-extraction/model-mgr/add-model',{
         project_id:id,
         model_name:this.addModelName,
         task_type:this.selectData,
         description:this.addModelDescription
       }).then((res:any)=>{
         console.log(res)
         this.isVisible = false;
         this.getModelList();
       }).catch((err: any) => {
        this.message.error(err.error_msg)
      })
    }
    
  }
  // 编辑模型
  EditModel(){
    this.EditModelName = this.EditModelName.replace(/^\s+|\s+$/g,"")
      if(this.EditModelName === '' ? this.message.error("编辑名称不能为空") : ''){
          return
      } else{
          this.http.post('/contract-extraction/model-mgr/update-model',{
            model_id : this.editModelId,
            description : this.Editdescribe,
            model_name: this.EditModelName
          }).then((res:any)=>{
            //查询模型列表
            this.getModelList()
          }).catch((err: any) => {
            this.message.error(err.error_msg)
          })
      }
  }
  // // 编辑描述按钮
  // EditDescribe(data){
  //   console.log(data)
  //   this.editId = data.model_id;
  //   this.EditModelName = data.model_name;
  //   this.Editdescribe = data.description
  // }
  // 编辑描述提交事件
  // EditInput(id){
  //   this.editId = '';
  //   this.editModelId = id;
  //   // || this.message.error(this.Editdescribe + '已经存在！')   
  //   let istrue = false 
  //   let newArray = this.listOfData.map(e => {
  //     if(id != e.model_id)
  //     return e.description
  //   });
  //   if (newArray.includes(this.Editdescribe)) {
  //     this.message.error(this.Editdescribe + '已经存在！') 
  //     return;
  //   }
  //     // 编辑模型接口
  //     this.EditModel();
     
  // }
  // 编辑名称按钮
  EditName(data){
    this.editNameId = data.model_id;
    this.EditModelName = data.model_name
    this.Editdescribe = data.description
    console.log(this.Editdescribe)
  }
  // 编辑名称提交事件
  EditInputName(id){
    this.editNameId = "";
    this.editModelId = id;
    // let istrue = false 
    let newArray = this.listOfData.map(e => {
      if(id != e.model_id)
      return e.model_name
    });
    if (newArray.includes(this.EditModelName)) {
      this.message.error(this.EditModelName + '已经存在!');
      return;
    }
      // 编辑模型接口
      this.EditModel()
  }
  // 删除模型
  deleteModel(e){
    this.deleteModelId = e;
    this.deleteShow = true;
  }
  // 取消删除
  deleteshut(){
    this.deleteShow = false;
  }
  // 确定删除
  deleteOk(){
    // 项目id
    let id = sessionStorage.getItem('id');
    this.http.post('/contract-extraction/model-mgr/delete-model',{
      project_id:id,
      model_id:this.deleteModelId
    }).then((res:any)=>{
      console.log(res)
      console.log(this.deleteModelId)
      this.deleteShow = false;
      // 调用模型管理列表 刷新数据
      this.getModelList()
    }).catch((err: any) => {
      this.message.error(err.error_msg)
    })
  }
  // 取消新建弹窗
  handleCancel() {
    this.isVisible = false;
  }
  // 展示新建弹窗
  showModal() {
    this.isVisible = true;
    // 每次进来清空输入框
    this.addModelName ='';
    this.addModelDescription = '';
  }
  province(e){
    console.log(e+'选择器')
    this.selectData = e;
    console.log(this.selectData)
  }
}
